; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=indvars -S  %s | FileCheck %s

define void @test_phi_select() {
; CHECK-LABEL: @test_phi_select(
; CHECK-NEXT:  entry:
; CHECK-NEXT:    br label [[OUTER_HEADER:%.*]]
; CHECK:       outer.header:
; CHECK-NEXT:    br i1 false, label [[THEN:%.*]], label [[MERGE:%.*]]
; CHECK:       then:
; CHECK-NEXT:    br label [[MERGE]]
; CHECK:       merge:
; CHECK-NEXT:    br label [[INNER:%.*]]
; CHECK:       inner:
; CHECK-NEXT:    [[INNER_IV_NEXT:%.*]] = add nsw i64 0, 0
; CHECK-NEXT:    br i1 false, label [[INNER]], label [[OUTER_LATCH:%.*]]
; CHECK:       outer.latch:
; CHECK-NEXT:    br i1 true, label [[EXIT:%.*]], label [[OUTER_HEADER]]
; CHECK:       exit:
; CHECK-NEXT:    ret void
;
entry:
  br label %outer.header

outer.header:
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %outer.latch ]
  %iv.next.1 = add nsw i64 %iv, 1
  %cmp.0 = icmp sgt i64 %iv, 50
  br i1 %cmp.0, label %then, label %merge

then:
  br label %merge

merge:
  %iv.sel = phi i64 [ %iv, %outer.header ], [ %iv.next.1, %then ]
  br label %inner

inner:                                            ; preds = %inner, %outer.header.new
  %inner.iv = phi i64 [ %iv.sel, %merge ], [ 0, %inner ]
  %inner.iv.next = add nsw i64 0, 0
  %ec.1 = icmp sgt i64 0, 0
  br i1 %ec.1, label %inner, label %outer.latch

outer.latch:
  %iv.next = add nuw nsw i64 %iv, 1
  %ec.2 = icmp ult i64 %iv.next, 1000
  br i1 %ec.2, label %exit, label %outer.header

exit:
  ret void
}
